#!/bin/bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

set -e

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/log.sh

. $LKP_SRC/lib/job.sh
load_job_vars queue testbox

check_params()
{
	log_info "start check params"

	local required_vars=(
		"iso_os"
		"iso_arch"
		"iso_version"
		"group_id"
		"VG_NAME"

		"HTTP_SERVER_HOST"
		"HTTP_SERVER_PORT"
		"HTTP_ROOT_DIR"
		"secrets_ROOTFS_ROOT_PASSWD"
		"rootfs_disk"

		"RSYNC_SERVER_HOST"
		"RSYNC_SERVER_PORT"
		"TFTP_SERVER_HOST"
		"NFS_SERVER_HOST"
		"NFS_ROOT_DIR"

		"LAB_Z9_URL"
		"secrets_MY_TOKEN"
	)

	for i in "${required_vars[@]}"
	do
		[ -n "$(eval echo \$$i)" ] || die "cannot get value of var: $i"
	done
}

prepare_vg()
{
	log_info "start create vg:$VG_NAME"

	local disk

	for disk in "${rootfs_disk[@]}"
	do
		log_info "start handle disk: $disk"
		pvdisplay ${disk} > /dev/null || pvcreate -y ${disk} || die "create pv failed: $disk"

		if vgdisplay ${VG_NAME} > /dev/null; then
			# if pv not in vg: add disk to vg
			pvdisplay $disk | grep 'VG Name' | grep -w $VG_NAME || {
				vgextend -y ${VG_NAME} ${disk} || die "vgextend failed: $disk"
			}
		else
			vgcreate -y ${VG_NAME} ${disk} || die "vgcreate failed: $disk"
		fi
	done
}

create_lv()
{
	log_info "start create lv:$LV_NAME"

	lvcreate -y -L 30G -n ${LV_NAME} ${VG_NAME} || die "create logical volume failed."

	mkfs.ext4 /dev/mapper/${VG_NAME}-${LV_NAME}
}

prepare_lv()
{
	log_info "start prepare logical volume"

	local iso_version=${iso_version//-/_}

	# activate lvm
	vgscan --cache
	vgchange -ay || vgchange -ay $VG_NAME

	# VG_NAME defines in job.yaml
	export VG_NAME
	export LV_NAME="${iso_os}_${iso_arch}_${iso_version}_${group_id}"

	# lv existed
	lvdisplay /dev/mapper/${VG_NAME}-${LV_NAME} > /dev/null && {
		log_info "iso already install to this testbox, so rootfs maybe already generated ok. please check it."
		exit 0
	}

	# lv not existed
	prepare_vg
	create_lv
}

generate_ks()
{
	log_info "start generate $ks_file"

	local nic=$(ip route get ${LKP_SERVER} | awk '{print $3; exit}')

	cat > ${ks_file} <<-EOF
	# Use network installation
	url --url="http://${HTTP_SERVER_HOST}:${HTTP_SERVER_PORT}/${HTTP_ROOT_DIR}/${iso_os}/${iso_arch}/${iso_version}/"

	# Network information
	network --bootproto=dhcp --device=${nic} --ipv6=auto --activate
	network  --hostname=localhost.localdomain

	# Specifies a list of disks for the installation program to use. All other disks are ignored.
	ignoredisk --only-use=${rootfs_disk#/dev/}

	# if installation target is a logical volume, bootloader should be none.
	bootloader --location=none
	logvol / --vgname=${VG_NAME} --name=${LV_NAME} --useexisting

	EOF

	curl -s http://${OS_HTTP_HOST}:${OS_HTTP_PORT}/os/install/ks/template/${iso_os}-${iso_arch}-${iso_version}.ks >> $ks_file
}

upload_ks()
{
	log_info "start upload ks to rsync-server: ${RSYNC_SERVER_HOST}:${RSYNC_SERVER_PORT}"

	rsync -vrtopg --port ${RSYNC_SERVER_PORT} ${ks_file} ${RSYNC_SERVER_HOST}::ks/ || die "upload ks to rsync-server failed, please check."
}

prepare_ks()
{
	log_info "start prepare kickstart file"

	local ks_file=/tmp/${iso_os}_${iso_arch}_${iso_version}.ks

	generate_ks
	upload_ks
}

prepare_submit()
{
	${LKP_SRC}/sbin/install-dependencies.sh

	export CCI_REPOS="/c"

	mkdir -p $CCI_REPOS && cd $CCI_REPOS
	git clone $LAB_Z9_URL || die "clone lab-z9 git repo failed: $LAB_Z9_URL"

	git clone https://gitee.com/compass-ci/lkp-tests.git
	cp -a $LKP_SRC/. ./lkp-tests/
	export LKP_SRC=$(realpath ./lkp-tests/)

	local config_yaml="/etc/compass-ci/defaults/sparrow.yaml"
	mkdir -p $(dirname $config_yaml)
	cat >> $config_yaml <<-EOF
	SCHED_HOST: ${SCHED_HOST}
	SCHED_PORT: ${SCHED_PORT}
	my_account: ${my_account}
	my_name: ${secrets_MY_NAME}
	my_email: ${secrets_MY_EMAIL}
	my_token: ${secrets_MY_TOKEN}
	lab: z9
	EOF
}

prepare_install_iso_yaml()
{
	log_info "start prepare install-iso.yaml"

	local tftp_root="tftp://${TFTP_SERVER_HOST}/EFI/BOOT/${iso_os}/${iso_arch}/${iso_version}"
	local custom_ipxe

	custom_ipxe="#!ipxe\\\\n"
	custom_ipxe="${custom_ipxe}\\\\ndhcp"
	custom_ipxe="${custom_ipxe}\\\\ninitrd ${tftp_root}/initrd.img"
	custom_ipxe="${custom_ipxe}\\\\nkernel ${tftp_root}/vmlinuz ro console=ttyS0 initrd=initrd.img ip=dhcp inst.ks=http://${HTTP_SERVER_HOST}:${HTTP_SERVER_PORT}/os/install/ks/${iso_os}_${iso_arch}_${iso_version}.ks"
	custom_ipxe="${custom_ipxe}\\\\nboot\\\\n"

	log_info "custom_ipxe: $custom_ipxe"
	sed -i "/custom_ipxe:/ccustom_ipxe: \"${custom_ipxe}\"" ${install_iso_yaml}
}

submit_install_iso_yaml()
{
	log_info "start submit install-iso.yaml"

	local install_iso_yaml="${LKP_SRC}/jobs/install-iso.yaml"
	[ -f "${install_iso_yaml}" ] || die "cannot find ${install_iso_yaml}"

	prepare_install_iso_yaml

	${LKP_SRC}/sbin/submit \
		${install_iso_yaml} \
		queue=$queue \
		testbox=$testbox || die "submit ${install_iso_yaml} failed"
}

submit_install_iso_firstboot_yaml()
{
	log_info "start submit install-iso-firstboot.yaml"

	local firstboot_yaml="${LKP_SRC}/jobs/install-iso-firstboot.yaml"
	[ -f "${firstboot_yaml}" ] || die "cannot find ${firstboot_yaml}"

	custom_ipxe="#!ipxe\\\\n"
	custom_ipxe="${custom_ipxe}\\\\ndhcp"
	custom_ipxe="${custom_ipxe}\\\\ninitrd http://${OS_HTTP_HOST}:${OS_HTTP_PORT}/os/${iso_os}/${iso_arch}/${iso_version}/initrd.lkp"
	custom_ipxe="${custom_ipxe}\\\\ninitrd http://${INITRD_HTTP_HOST}:${INITRD_HTTP_PORT}/initrd/custom_bootstrap/reboot-after-systemd-logind.cgz"
	custom_ipxe="${custom_ipxe}\\\\nkernel http://${OS_HTTP_HOST}:${OS_HTTP_PORT}/os/${iso_os}/${iso_arch}/${iso_version}/boot/vmlinuz ip=dhcp root=/dev/mapper/${VG_NAME}-${LV_NAME} initrd=initrd.lkp initrd=reboot-after-systemd-logind.cgz local rw"
	custom_ipxe="${custom_ipxe}\\\\nboot\\\\n"

	sed -i "/custom_ipxe:/ccustom_ipxe: \"${custom_ipxe}\"" $firstboot_yaml

	${LKP_SRC}/sbin/submit \
		${firstboot_yaml} \
		suite=install-iso-firstboot \
		group_id=${group_id} \
		queue=$queue \
		testbox=$testbox || die "submit ${firstboot_yaml} failed"
}

submit_install_iso_sync_rootfs_yaml()
{
	log_info "start submit install-iso-sync-rootfs.yaml"

	local sync_rootfs_yaml="${LKP_SRC}/jobs/install-iso-sync-rootfs.yaml"
	[ -f "${sync_rootfs_yaml}" ] || die "cannot find ${sync_rootfs_yaml}"

	${LKP_SRC}/sbin/submit \
		${sync_rootfs_yaml} \
		suite=install-iso-sync-rootfs \
		group_id=${group_id} \
		install-iso-sync-rootfs.iso_os=${iso_os} \
		install-iso-sync-rootfs.iso_arch=${iso_arch} \
		install-iso-sync-rootfs.iso_version=${iso_version} \
		install-iso-sync-rootfs.NFS_SERVER_HOST=${NFS_SERVER_HOST} \
		install-iso-sync-rootfs.NFS_ROOT_DIR=${NFS_ROOT_DIR} \
		install-iso-sync-rootfs.VG_NAME=${VG_NAME} \
		queue=$queue \
		testbox=$testbox || die "submit job failed: ${sync_rootfs_yaml}"
}

main()
{
	check_params

	prepare_lv
	prepare_ks
	prepare_submit

	submit_install_iso_yaml
	submit_install_iso_firstboot_yaml
	submit_install_iso_sync_rootfs_yaml
}

main
